package com.jbit.hxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.mapper.*;
import com.jbit.hxy.model.dto.DishFromDTO;
import com.jbit.hxy.model.dto.DishQueryDTO;
import com.jbit.hxy.model.po.WxCategory;
import com.jbit.hxy.model.po.WxDish;
import com.jbit.hxy.model.po.WxDishFlavor;
import com.jbit.hxy.model.po.WxSetmeal;
import com.jbit.hxy.model.vo.DishDetailVO;
import com.jbit.hxy.model.vo.PageVO;
import com.jbit.hxy.model.vo.WxDishFlavorVO;
import com.jbit.hxy.service.menu.WxDishService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 28575
* @description 针对表【wx_dish(菜品)】的数据库操作Service实现
* @createDate 2024-09-07 16:49:34
*/
@Service
@RequiredArgsConstructor
public class WxDishServiceImpl extends ServiceImpl<WxDishMapper, WxDish>
    implements WxDishService {
    private final WxDishMapper wxDishMapper;
    private final SqlSessionFactory sqlSessionFactory;
    private final CategoryDishMapper categoryDishMapper;
    private final WxCategoryMapper wxCategoryMapper;
    private final CategorySetmealMapper categorySetmealMapper;
    private final WxSetmealMapper wxSetmealMapper;
    private final DishFlavorMapper dishFlavorMapper;
    private final WxDishFlavorMapper wxDishFlavorMapper;

    @Override
    public Result getDishes(DishQueryDTO dishQueryDTO) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            // 计算偏移量
            int offset = (dishQueryDTO.getCurPage() - 1) * dishQueryDTO.getPageSize();

            // 准备参数
            Map<String, Object> params = new HashMap<>();
            params.put("name", dishQueryDTO.getName());
            params.put("offset", offset);
            params.put("limit", dishQueryDTO.getPageSize());

            // 获取总记录数
            int total = sqlSession.selectOne("com.jbit.hxy.mapper.WxDishMapper.countWxDishesByConditions", params);

            // 执行 SQL 查询
            List<WxDish> wxDishes = sqlSession.selectList("com.jbit.hxy.mapper.WxDishMapper.getWxDishesByConditions", params);

            // 创建 PageInfo 对象
//            PageInfo<Leave> pageInfo = new PageInfo<>(leaves);
//            pageInfo.setTotal(total);
//            System.out.println(total);
            PageVO pageVo = new PageVO<>();

            pageVo.setTotal(total);
            pageVo.setList(wxDishes);

            pageVo.setPageSize(dishQueryDTO.getPageSize());
            pageVo.setCurrentPage(dishQueryDTO.getCurPage());
            return Result.success(pageVo);
        }
    }

    @Override
    public Result addDishes(DishFromDTO dishFromDTO) {

        WxDish wxDish = new WxDish();
        BeanUtils.copyProperties(dishFromDTO,wxDish);
        wxDish.setCreatetime(LocalDateTime.now());
        wxDish.setUpdatetime(LocalDateTime.now());
        wxDishMapper.insert(wxDish);
        return Result.success("添加成功");
    }

    @Override
    public Result updateDishes(DishFromDTO dishFromDTO) {

        WxDish wxDish = new WxDish();
        BeanUtils.copyProperties(dishFromDTO,wxDish);
        wxDish.setUpdatetime(LocalDateTime.now());
        wxDishMapper.updateById(wxDish);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteDishes(Integer id) {

        wxDishMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result getDishBycateId(Integer cateId) {
        WxCategory wxCategory = wxCategoryMapper.selectById(cateId);
        if (wxCategory.getType() == 1) {
            List<Integer> integers = categoryDishMapper.selectByCateId(cateId);
            if (integers.isEmpty()) {
                return Result.success(new ArrayList<WxDish>());

            }
            List<WxDish> wxDishes = wxDishMapper.selectBatchIds(integers);
            return Result.success(wxDishes);
        } else {
            List<Integer> integers = categorySetmealMapper.selectByCateId(cateId);
            if (integers.isEmpty()) {
                return Result.success(new ArrayList<WxSetmeal>());
            }
            List<WxSetmeal> wxSetmeals = wxSetmealMapper.selectBatchIds(integers);
            return Result.success(wxSetmeals);
            }
        }

    @Override
    public Result getAllDishes() {
        List<WxDish> wxDishes = wxDishMapper.selectList(null);
        return Result.success(wxDishes);
    }

    @Override
    public Result getDishDetail(Integer id) {
        WxDish wxDish = wxDishMapper.selectById(id);
        DishDetailVO dishDetailVO = new DishDetailVO();
        BeanUtils.copyProperties(wxDish,dishDetailVO);
        dishDetailVO.setNum(1);
        List<Integer> flavorIds = dishFlavorMapper.getFlavorIdByDishId(id);
        ArrayList<WxDishFlavorVO> wxDishFlavorVOS = new ArrayList<>();
        for (Integer flavorId : flavorIds) {
            WxDishFlavor wxDishFlavor = wxDishFlavorMapper.selectFlavorById(flavorId);
            String values = wxDishFlavor.getValues();
            Gson gson = new Gson();
            Type listType = new TypeToken<List<String>>() {}.getType();
            List<String> sugarLevels = gson.fromJson(values, listType);
            WxDishFlavorVO wxDishFlavorVO = new WxDishFlavorVO();
            BeanUtils.copyProperties(wxDishFlavor,wxDishFlavorVO);
            wxDishFlavorVO.setValues(sugarLevels);

            wxDishFlavorVOS.add(wxDishFlavorVO);


        }
        dishDetailVO.setWxDishFlavors(wxDishFlavorVOS);
        return Result.success(dishDetailVO);
    }

    @Override
    public Result getDishNameById(Integer dishId) {

        WxDish wxDish = wxDishMapper.selectById(dishId);
        return Result.success(wxDish.getName());
    }


}




